/*********************
* Author:Dzlua
* QQ:505544956
* time:2016/06/27
*/
#ifndef __EDUI_SDL_ENGINE_H__
#define __EDUI_SDL_ENGINE_H__

#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include "edui/interface/IEngine.h"
#include "Window.h"

EDUI_NAMESPACE_BEGIN

class Engine : public IEngine
{
public:
    Engine();
    virtual ~Engine();
public:
    virtual bool Init(const void* data1 = 0, const void* data2 = 0);
    virtual void Quit();
    virtual void PushMsg(Msg *msg);

    virtual IWindow*  GetWindowInterface();
    virtual ITexture* GetTextureInterface();
    virtual ISurface* GetSurfaceInterface();
    virtual IFont*    GetFontInterface();
    virtual ICursor*  GetCursorInterface();

    virtual const char* GetVersion();

    virtual bool SetClipboardText(const char* text);//clipboard text
    virtual char* GetClipboardText();
    virtual bool HasClipboardText();

    // 0 will set default cursor
    virtual void SetCursor(ICursor* cursor);
    virtual void ShowCursor(bool show);
    virtual bool IsCursorShow();

    //if wnd,PushMsg.
    virtual TimerID AddTimer(Uint32 interval, TimerCallback callback,IWindow* wnd = 0, void *data1 = 0, void *data2 = 0);
    virtual void RemoveTimer(TimerID id);
    virtual void RemoveTimer(IWindow *wnd);
public:
    //该类的基类转换为该类(基类必须为该派生类转换过去的)
    static Engine* ToEngine(IEngine* engine);

    /* fps:
    *       < 0 : waite for msg.
    *       = 0 : as fast as possible.
    *       > 0 : fixed fps.
    */
    virtual void RunMsgLoop(float fps = -1);

    IWindow* GetWindowBySDLWindow(SDL_Window* wnd);
    virtual IWindow* GetKeyFocusWindow();
    virtual IWindow* GetMouseFocusWindow();

protected:
    /* handle SDL2 msg and dispatch it to edui msg
    *   Engine::HandleSDLMsg() -> IEngine::DispatchMsg() -> IWindow::HandleMsg()
    *   SDL2 msg                    edui msg                    handle edui msg.
    */
    void HandleSDLMsg(SDL_Event* event);
protected:
    virtual void onInit();
    virtual void onQuit();
protected:
    MgrSimple<TimerInfo> m_timerInfos;
};

EDUI_NAMESPACE_END

#endif